package com.ktjiaoyu.ch01_hello.service;

import com.ktjiaoyu.ch01_hello.pojo.User;
import com.ktjiaoyu.ch01_hello.repository.UserRepository;
import jakarta.annotation.Resource;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserRepository userRepository;

    @Cacheable(value="t365users",keyGenerator = "keyGenerator")
    public User findById(Long usrId){
        return userRepository.findById(usrId).get();
    }

    @Override
    public Page<User> findPageByMap(Map<String, Object> param, Pageable pageable) {

        return null;
       /* userRepository.findAll(new Specification<User>(){

            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList=new ArrayList<>();

                if (param.get("usrName")!=null){
                    predicateList.add(cb.like(root.get("usrName"),"%"+param.get("usrName")+"%")); //usrName  like #{usrName}
                }
              *//*  if(param.get("usrRoleId")!=null){
                    predicateList.add(cb.equal(root.get("usrRoleId"),param.get("usrRoleId")));
                }*//*

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        },pageable);*/

       // return null;
    }
}
